package com.school.dao.impl;

import com.school.config.DbcpConfig;
import com.school.dao.BaseDao;

import java.sql.*;
import java.util.Collection;
import java.util.List;

public abstract class BaseDaoImpl<T> implements BaseDao<T> {

    protected String tableName = getClass().getSimpleName().replace("DaoImpl", "").toLowerCase();

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    @Override
    public abstract T insert(T t);

    @Override
    public abstract List<T> insert(List<T> ts);

    @Override
    public int delete(Integer id) {
        String sql = "delete from " + tableName + " where id = ?";
        try {
            PreparedStatement preparedStatement = DbcpConfig.getPreparedStatement(sql);
            if (preparedStatement != null) {
                preparedStatement.setInt(1, id);
                int len = preparedStatement.executeUpdate();
                preparedStatement.close();
                return len;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int[] delete(Collection<Integer> ids) {
        String sql = "delete from " + tableName + " where id = ?";
        try {
            Connection connection = DbcpConfig.getConnection();
            // 开启事务
            connection.setAutoCommit(false);
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            if (preparedStatement != null) {
                for (Integer id : ids) {
                    preparedStatement.setInt(1, id);
                    // 添加一批
                    preparedStatement.addBatch();
                }
                // 批量执行
                int[] count = preparedStatement.executeBatch();
                // 提交事务
                connection.commit();
                preparedStatement.close();
                return count;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return new int[0];
    }

    @Override
    public abstract int update(T t);

    @Override
    public abstract int[] update(List<T> ts);

    @Override
    public int count() {
        String sql = "select count(*) as count from " + tableName;
        try {
            Statement statement = DbcpConfig.getStatement();
            if (statement != null) {
                ResultSet res = statement.executeQuery(sql);
                int count = 0;
                while (res.next()) {
                    count = res.getInt("count");
                }
                statement.close();
                return count;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public abstract T findOneById(Integer id);

    @Override
    public abstract List<T> findPage(T t, Integer offset, Integer size);
}
